home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / pipeline / abacus / p_line / Custom01 / ReadMe < prev   
Encoding:
Text File  |  1992-08-14  |  15.1 KB  |  286 lines

  1. %OP%VS4.13 (28-Apr-92), Gerald L Fitton, R4000 5966 9904 9938 
  2. %OP%DP0
  3. %OP%IRY
  4. %OP%PL0
  5. %OP%HM0
  6. %OP%FM0
  7. %OP%BM0
  8. %OP%LM4
  9. %OP%FX160
  10. %OP%FY160
  11. %OP%FS10000
  12. %OP%PT1
  13. %OP%PDPipeLine
  14. %OP%WC1022,2258,184,1748,0,0,0,0
  15. %CO:A,72,72%
  16. %C%Custom Functions from the Beginning - Part 1
  17. %C%by Gerald L Fitton
  18. Keywords:
  19. 4ProL Custom Function Introduction Parameter Sequence Fitton
  20.  
  21. Introduction to '4ProL'
  22. This is the first tutorial article of a series which, I hope, will help 
  23. you to get to grips with one of the new features of PipeDream 4, custom 
  24. functions.  If you wish to use these custom functions then you need to 
  25. learn the PipeDream 4 custom function programming language which, at 
  26. present, doesn't have a name.  I shall call it '4ProL'.  I'm not sure 
  27. of the best pronunciation but 'four-prol' seems OK to me!  When Robert 
  28. Macmillan reads this I am sure that he will offer a small prize for the 
  29. best name (and pronunciation) of PD4's programming language.
  30.  
  31. One thing to clear up from the start.  Custom functions are different 
  32. from command files.  Custom functions did not exist in PipeDream 3 but 
  33. command files (called 'macros') did.  Command files consist of a 
  34. sequence of PipeDream commands such as <Ctrl CGS> (Cursor Goto Slot) 
  35. and <Ctrl BM> (Block Move); they can be used to add or delete rows and 
  36. columns, even to load and save documents.  Custom functions can not be 
  37. used to move blocks around within a document nor indeed to change the 
  38. 'shape' of a document in any way.  However, what you can do with custom 
  39. functions is to process the data which already exists within the slots 
  40. of a document (or within a set of documents) in more intricate ways 
  41. than can be done simply with the 'standard' functions provided.  Just 
  42. like any standard function such as (A1 + A2) or the more complex 
  43. lookup(A1,B1B10,C1C10), a custom function is 'called' from a slot and 
  44. the 'result' of executing the function is returned to the slot from 
  45. which it is called.
  46.  
  47. Calling custom functions
  48. Let's have a look at a custom function at work.  Double click on the 
  49. file [AddOne] and you will find that you load to the screen not only 
  50. [AddOne] but a dependent document which I have called [c_AddOne] 
  51. containing a simple, two line, custom function.  I have deliberately 
  52. limited the width of [c_AddOne] to 72 characters so that it will fit 
  53. across a mode 12 screen.
  54.  
  55. You can use any valid filing system file name for a custom function 
  56. document but, so that I can recognise which PipeDream files are 
  57. 'ordinary' documents and which are custom function documents, I have 
  58. prefixed all my custom function documents with c_ (a lower case c 
  59. followed by an underline).  I suggest that you follow this convention; 
  60. it is recommended not only by me but by Colton Software as well.
  61.  
  62. Because PipeDream 4 saves the position of a PipeDream document on the 
  63. screen and the position of the cursor within the document, what you 
  64. should find is that [AddOne] is visible and alive (a yellow top bar) 
  65. with the cursor in slot [AddOne]A3.  If not then position the pointer 
  66. on the number in [AddOne]A3 (it should be the number 2 unless you've 
  67. modified the file) and click the mouse select (left) button.  You 
  68. should see the same number in the formula line (at the top of the 
  69. document just to the right of the cross and tick).  Tap some other 
  70. number, say 9, and then tap <Return>.  If all goes well then, besides 
  71. [AddOne]A3 changing to 9, the number in [AddOne]B3 will change to 10.
  72.  
  73. The custom function 'called' from [AddOne]B3 is not a spectacular 
  74. custom function.  The 'result' returned to the slot [AddOne]B3 is 
  75. (A3 + 1), one more than the number in the slot [AddOne]A3.  Change the 
  76. value in [AddOne]A3 a few times and convince yourself that the value in 
  77. [AddOne]B3 is always 1 more than the value in [AddOne]A3.
  78.  
  79. Now let's have a look at the content of slot [AddOne]B3.  Place the 
  80. pointer over [AddOne]B3 and click select.  The formula line does not 
  81. show the simple, non custom function way of adding 1 which would be 
  82. (A3 + 1); instead it shows the formula used to 'call' the custom 
  83. function, namely [c_AddOne]one_more_than(A3).  Before looking at the 
  84. custom function document, [c_AddOne], let's have a look at the formula 
  85. used to call the custom function in more detail.
  86.  
  87. The 'calling' formula is in three parts:
  88.  
  89. The first part - [c_AddOne] - is the name of the dependent document 
  90. which contains the custom function.  A custom function document such as 
  91. [c_AddOne] can contain more than one custom function.  Indeed, it is 
  92. desirable to 'split up' large, exotic custom functions into a set of 
  93. smaller custom functions (stored within the same 'c_' document) in the 
  94. same way that it is usually desirable to split up a long program 
  95. written in BASIC into more easily digested PROCs and FNs.
  96.  
  97. The second part - one_more_than - is the name of the custom function 
  98. within [c_AddOne].  If you look at slot A7 of the document [c_AddOne] 
  99. you will see that - one_more_than -, the name of the custom function, 
  100. appears after the word 'function'.  Another convention which I 
  101. recommend to you is that the names of custom functions should be 
  102. totally in lower case (no capitals).  The reason is that lower case and 
  103. upper case function names are equivalent (unlike BASIC procedures) and 
  104. PipeDream converts upper case letters in the custom function names to 
  105. lower case anyway!
  106.  
  107. The third part - (A3) - is the single piece of data which is passed to 
  108. the custom function for processing.  To be more accurate, [AddOne]A3 is 
  109. the slotref of the data passed to [c_AddOne] for processing.  The data 
  110. passed to a custom function this way is called a 'parameter'.  A 
  111. parameter is a 'sort of' variable which has a 'fixed' value within the 
  112. custom function but can be varied outside the custom function.  As we 
  113. shall see in a later tutorial, it is possible to pass more than one 
  114. parameter to a custom function.  You can pass as many different 
  115. parameters as you wish to a custom function but, if you want to pass a 
  116. large amount of data, rather than use many parameters (a rather 
  117. cumbersome method), it is better to pass a range of slots or an array 
  118. as one single parameter.
  119.  
  120. Returning the result
  121. 'Calling' the custom function - [c_AddOne]one_more_than(A3) - from slot 
  122. [AddOne]B3 returns the result of the processing to the slot from which 
  123. it was called, [AddOne]B3.  Although the result can be returned to only 
  124. a single slot this does not mean that you can return only one value 
  125. from a custom function!  You can return many values to one slot by 
  126. returning an array to the calling slot.  This array can then be 
  127. expanded using set_value(range,slotref) where 'slotref' contains the 
  128. array (and the calling function) and where 'range' is the range of 
  129. slots containing the expanded array.  We shall return to examples of 
  130. this tactic in a later tutorial.
  131.  
  132. Programming in general
  133. Before we look at the custom function [c_AddOne] in detail I am going 
  134. to digress and discuss programming from a broader point of view.
  135.  
  136. Perhaps you are familiar with writing short programs in BASIC.  That 
  137. may help or it may not!  Perhaps you write 'well written' or perhaps 
  138. 'badly written' BASIC programs.  Both types usually work most of the 
  139. time; both types sometimes fail.  The difference between the two is 
  140. often apparent only when you want to 'debug', modify, improve or extend 
  141. the program you (or maybe someone else) has written.  Those programs 
  142. which I class as 'good' are usually easier to 'debug' if they don't 
  143. work first time and easier for others to modify if they wish to extend 
  144. or improve them.  Short 'bad' programs can usually be written in a much 
  145. shorter time so too many programmers with no formal training pick up 
  146. some bad habits when they start programming.  This is particularly true 
  147. if they write short 'bad' programs which work.  Early 'bad' habits 
  148. learnt that way are often the most difficult to break.
  149.  
  150. Some languages encourage 'good' programming.  Others permit 'bad' or 
  151. even 'very bad' programming.  I think I would put '4ProL' in the 
  152. 'fairly good' category!
  153.  
  154. Programming in '4ProL'
  155. Custom function documents such as - [c_AddOne] - contain custom 
  156. functions such as - one_more_than - which are written in the 
  157. programming language I have called '4ProL'.  A custom function is a set 
  158. of instructions which is designed to process similar data in a similar 
  159. way.  No matter what the actual value of the data, the same set of 
  160. instructions are followed.  For example, in the case of 
  161. - one_more_than - the data passed to it is a parameter representing a 
  162. single number such as 2 and the pair of instructions which make up 
  163. - one_more_than - process the number represented by the parameter by 
  164. adding 1 and returning the result.
  165.  
  166. Whatever the number which is passed as a parameter to - one_more_than - 
  167. the 'result' returned is one more than the original number.  That 
  168. original number is represented within the custom function as the 
  169. parameter "parameter".  Custom functions are written so that they can 
  170. and most often do operate on variables (symbols representing any 
  171. similar piece of data) rather than sets of instructions which process 
  172. data directly.
  173.  
  174. There are many ways of classifying variables; one is by the way in 
  175. which they retain or lose their identity when the overall program is 
  176. broken down into smaller units.  Using this classification a parameter 
  177. is a 'sort of' variable; there are two more sorts called 'local' and 
  178. 'global' variables which I shall introduce in another tutorial.
  179.  
  180. Within all but the simplest programming languages the manner of 
  181. processing the data (best included as variables) is based on four 
  182. concepts.  We shall study these concepts under the headings: 
  183. "Sequence", "Repetition" (such as "for - next" loops), "Decision" (such 
  184. as "if - then" statements) and "Interrupt" (which will include the use 
  185. of the '4ProL' commands - input - and - alert).  I shall limit this 
  186. tutorial to a discussion of "Sequence" and the use of a parameter.  
  187. In later tutorials we shall discover other sorts of variables as well 
  188. as the concepts of "Repetition", "Decision" and "Interrupt".
  189.  
  190. Sequence
  191. This topic is concerned with the order in which the commands are 
  192. executed.  Every sequence must have a start and a finish.
  193.  
  194. In a large number of spreadsheets you must choose whether to 
  195. recalculate along the rows (one at a time, starting at the top and 
  196. running from left to right along each row) or down the columns (one at 
  197. a time starting with the left most column and working down each column 
  198. from top to bottom).  In PipeDream 4 recalculation takes place in an 
  199. order which is called "natural".  Here is not the place to explain in 
  200. detail what is meant by "natural"; essentially, with "natural" 
  201. recalculation, every slot can be regarded as part of a chain and the 
  202. calculation proceeds along the chain in such a way that slots which 
  203. depend on other slots are processed last no matter where they are 
  204. located within the document.  When a slot is changed in PipeDream 4 
  205. only those chains which are affected are recalculated.
  206.  
  207. In a PipeDream 4 custom function the default sequence is that commands 
  208. are executed, one at a time, down a single column.  The start is 
  209. identified by the command - function - and the end is identified by the 
  210. command - result.
  211.  
  212. The custom function
  213. What we haven't looked at yet is the custom function itself to see how 
  214. it adds one to the parameter and returns the 'result'.  To do this we 
  215. must bring the custom function - [c_AddOne]one_more_than - into view.
  216.  
  217. Let's look at the commands in detail and see how they process data.
  218.  
  219. Row 7 - ...function("one_more_than","parameter:number")
  220. In this custom function the '4ProL' command - ...function - has two 
  221. arguments.  The first argument is the name of the custom function, 
  222. - "one_more_than" - note that the name is included in inverted commas.  
  223. The second argument is the one and only parameter passed to the 
  224. function.  The name of the one parameter passed to - [c_AddOne] - is 
  225. - parameter - and its 'type' has been declared (after the colon) as a 
  226. number.  Because of this declaration, if you try to pass anything other 
  227. than a number to this function then an error will be generated.  You do 
  228. not have to declare the type of variable if you want to pass a number 
  229. sometimes and, say, a string on another occasion.
  230.  
  231. Try typing Fred (without and then with inverted commas) into [AddOne]A3 
  232. and you will find that the error message - String not expected - is 
  233. returned to slot [AddOne]B3.  You will find the error message 
  234. foreshortened in the body of the [AddOne] document.  If you want to 
  235. read the full error message then click the pointer in [AddOne]B3, then 
  236. on the formula button (the italic f just to the right of the 
  237. PipeDream 4 logo) and finally run the pointer through the first menu 
  238. option - Slot 'B3' - Slot value - and you will see the full error 
  239. message displayed.  This technique is particularly useful when long 
  240. error messages are generated.
  241.  
  242. Row 8 - ...result(@parameter+1)
  243. The command - ...result - terminates the sequence of commands and 
  244. returns a value to the slot from which it was called, [AddOne]B3.  Note 
  245. the @@ sign preceding the word - parameter - and the inverted commas 
  246. have gone.  The @@ sign 'goes with' the word - parameter - try 
  247. ...return(1+@@parameter), it gives the same result.
  248.  
  249. Try changing ...return(@parameter+1) to ...return(@parameter+2).  You 
  250. have to do this in the formula line of [c_AddOne].  Even when you have 
  251. modified the custom function, the number 3 in the slot [AddOne]B3 does 
  252. not change.  To make that change, and indeed if you want to check 
  253. whether you have introduced any 'bugs' (errors in programming) in 
  254. [c_AddOne] you must 'run' the custom function by 'calling' it again 
  255. from the document [AddOne].  The simplest way of doing this is to place 
  256. the cursor in slot [AddOne]B3, then move the pointer to the formula 
  257. line and click on [c_AddOne]one_more_than(A3).  Finally, click on the 
  258. green tick to the left of the formula line; when you click on the green 
  259. tick the custom function will be called and the new 'result' will be 
  260. returned to [AddOne]B3.
  261.  
  262. Over to you now
  263. As your first exercise in writing custom functions try to recreate the 
  264. pair of documents, [AddOne] and [c_AddOne].  Only when you have 
  265. succeeded are you ready to continue!  If you have problems then reload 
  266. my [c_AddOne] and type <Ctrl O> to check that you have suitable default 
  267. options in your custom function document.  In particular it is 
  268. advisable to set the <Ctrl O> - New slot format - to Numbers.  
  269. Secondly, note that the three dots which precede the word - function - 
  270. appear automatically in slot [c_AddOne]A7 and do not appear in the 
  271. formula line.  You do not type the three dots anywhere; just type the 
  272. expression - function("one_more_than","parameter:number") - into a 
  273. "number" slot and tap <Return>!  In case you really get stuck I have 
  274. included in this directory a file call [CustomFn] which you might like 
  275. to load and then save with <Ctrl FI> as a custom function template.
  276.  
  277. Summary
  278. A custom function is a sequence of commands which start with a 
  279. - function - command and end with a - result - command.  Once there is 
  280. a custom function within a document then the whole document is a custom 
  281. function document.  Custom function documents behave differently from 
  282. 'ordinary' documents - but they do not have a different file type!
  283.  
  284. The next tutorial
  285. The next tutorial of this series is in the Custom02 directory.
  286.